﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.PersonalInfo
{
    public partial class BasicInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID; string msg = string.Empty;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);
            //-------------------------------------------------------------------------------------------------
            //个人主信息
            Jy123.Model.User_Main mUser = new Jy123.Model.User_Main();
            Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();
            mUser = bllUser.GetModel(userID);
            //个人详细信息
            Jy123.Model.User_Info mUserInfo = new Jy123.Model.User_Info();
            Jy123.BLL.User_Info bllUserInfo = new Jy123.BLL.User_Info();
            mUserInfo = bllUserInfo.GetModel(userID);

            string strSave = Request.Form["save"];
            if (strSave == "保存基本资料")
            {
                try
                {
                    string strSex = Request.Form["sex"];
                    string strBirth = Request.Form["birth"];
                    string strBirthPrivacy = Request.Form["birthPrivacy"];
                    string strConstellation = Request.Form["constel"];
                    string strProvince = Request.Form["selProvince"];
                    string strCity = Request.Form["selCity"];
                    string strVocation = Request.Form["vocation"];
                    string strMarriage = Request.Form["Marriage"];
                    if (!string.IsNullOrEmpty(strBirth.Trim()) & !string.IsNullOrEmpty(strBirthPrivacy.Trim()) & !string.IsNullOrEmpty(strConstellation.Trim()) &
                        !string.IsNullOrEmpty(strProvince.Trim()) & !string.IsNullOrEmpty(strCity.Trim()) & !string.IsNullOrEmpty(strVocation.Trim()) &
                         !string.IsNullOrEmpty(strMarriage.Trim()))
                    {
                        int intSex = Convert.ToInt32(strSex);
                        mUserInfo.Sex = Convert.ToBoolean(intSex);
                        mUserInfo.Birthday = Convert.ToDateTime(strBirth);
                        mUserInfo.BirthidayDisplay = Convert.ToInt32(strBirthPrivacy);
                        mUserInfo.Marriage = Convert.ToInt32(strMarriage);
                        mUserInfo.Constellation = Convert.ToInt32(strConstellation);
                        mUserInfo.Vocation = Convert.ToInt32(strVocation);
                        mUserInfo.HomeProv = Convert.ToInt32(strProvince);
                        mUserInfo.HomeCity = Convert.ToInt32(strCity);
                        bllUserInfo.Update(mUserInfo);
                        msg = "<script>$(function(){$.moonbox.ok('修改成功!');})</script>";
                        //加入动态
                        Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                        Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                        mUserForState = bllUserForState.GetModel(userID);
                        new Jy123.BLL.Public.State().CreateModifyUserState(mUserForState,null, (int)Jy123.BLL.Public.EnumConfig.StateActionType.ModifyUserInfo);
                        //加入动态
                    }
                    else
                    { msg = "<script>$(function(){$.moonbox.error('有信息未填写，请填写完整!');})</script>"; }
                    
                }
                catch (Exception ex)
                {
                    throw ex;
                    //msg = "<script>$(function(){$.moonbox.error('"+ex+"');})</script>";
                }
            }
            #region 页面初始化
            StringBuilder sbHtml = new StringBuilder(Cache["user_basicinfo"].ToString());//最终写出页面的大sb
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友
            
            //-------------------------------------------------------------------------------
            if (mUserInfo.HomeProv == null) { mUserInfo.HomeProv = 0; }
            if (mUserInfo.HomeCity == null) { mUserInfo.HomeCity = 0; }
            if (mUserInfo.BirthidayDisplay == null) { mUserInfo.BirthidayDisplay = 0; }
            if (mUserInfo.Constellation == null) { mUserInfo.Constellation = 0; }
            if (mUserInfo.Vocation == null) { mUserInfo.Vocation = 0; }
            if (mUserInfo.Marriage == null) { mUserInfo.Marriage = 0; }

            sbHtml = sbHtml.Replace("$UserProvinceID$",mUserInfo.HomeProv.ToString());
            sbHtml = sbHtml.Replace("$UserCityID$",mUserInfo.HomeCity.ToString());
            //判断性别
            switch (mUserInfo.Sex)
            {
                case true:
                    sbHtml = sbHtml.Replace("$checkedBoy$", "");
                    sbHtml = sbHtml.Replace("$checkedGirl$", "checked");
                    break;
                case false:
                    sbHtml = sbHtml.Replace("$checkedBoy$", "checked");
                    sbHtml = sbHtml.Replace("$checkedGirl$", "");
                    break;
            }
            
            sbHtml = sbHtml.Replace("$BrithDay$",((DateTime)mUserInfo.Birthday).ToString("yyyy-MM-dd"));    //生日            
            sbHtml = DisplayBirthday((int)mUserInfo.BirthidayDisplay, sbHtml);          //生日显示样式 
             
            sbHtml = sbHtml.Replace("$ConstellationOptions$",UserConstellation(Convert.ToInt32( mUserInfo.Constellation))); //星座            
            sbHtml = sbHtml.Replace("$VocationOption$",UserVocation((int)mUserInfo.Vocation));  //行业            
            sbHtml = Marriage((int)mUserInfo.Marriage, sbHtml); //婚姻状态

            #endregion
            Response.Write(sbHtml.ToString()+msg);
        }
        /// <summary>
        /// 星座选项
        /// </summary>
        /// <param name="userConsID">用户的星座ID</param>
        /// <returns>字符串</returns>
        private string UserConstellation(int userConsID)
        {
            StringBuilder sbConsOption = new StringBuilder();
            string strConsOption = Cache["user_constellation_option"].ToString();
            List<Jy123.Model.User_Constellation> listConstellation = new Jy123.BLL.User_Constellation().GetModelList("");
            foreach (Jy123.Model.User_Constellation consOption in listConstellation)
            {
                string str = strConsOption;                
                if (consOption.Id == userConsID) 
                {
                    str = str.Replace("$ConstellationID$", consOption.Id.ToString()); 
                }
                else 
                { 
                    str = str.Replace("$ConstellationID$", consOption.Id.ToString());
                    str = str.Replace("selected=selected","");
                }
                str = str.Replace("$ConstellationName$", consOption.Constellation);
                sbConsOption.Append(str);
            }
            return sbConsOption.ToString();
        }
        /// <summary>
        /// 生日显示样式
        /// </summary>
        /// <param name="BirthidayDisplayID">显示样式的ID</param>
        /// <param name="sbHtml">主StringBuilder</param>
        /// <returns></returns>
        private StringBuilder DisplayBirthday(int BirthidayDisplayID,StringBuilder sbHtml)
        {
            switch (BirthidayDisplayID)
            {
                case 0: 
                    sbHtml = sbHtml.Replace("$selected_yymmdd$","selected")
                        .Replace("$selected_mmdd$","")
                        .Replace("$selected_yy$", "")
                        .Replace("$selected_null$", "");
                    break;
                case 1:
                    sbHtml = sbHtml.Replace("$selected_yymmdd$", "")
                        .Replace("$selected_mmdd$", "selected")
                        .Replace("$selected_yy$", "")
                        .Replace("$selected_null$", "");
                    break;
                case 2:
                    sbHtml = sbHtml.Replace("$selected_yymmdd$", "")
                        .Replace("$selected_mmdd$", "")
                        .Replace("$selected_yy$", "selected")
                        .Replace("$selected_null$", "");
                    break;
                case 3:
                    sbHtml = sbHtml.Replace("$selected_yymmdd$", "")
                        .Replace("$selected_mmdd$", "")
                        .Replace("$selected_yy$", "")
                        .Replace("$selected_null$", "selected");
                    break;
            }
            return sbHtml;
        }
        /// <summary>
        /// 行业选项
        /// </summary>
        /// <param name="userVocID">用户行业的ID</param>
        /// <returns>字符串</returns>
        private string UserVocation(int userVocID)
        {
            StringBuilder sbVocOption = new StringBuilder();
            string strVocOption = Cache["user_vocationoption"].ToString();
            List<Jy123.Model.User_Dict_Vocation> listVocation = new Jy123.BLL.User_Dict_Vocation().GetModelList("");
            foreach (Jy123.Model.User_Dict_Vocation mVocation in listVocation)
            {
                string str = strVocOption;

                if (mVocation.ID == userVocID)
                {
                    str = str.Replace("$VocationOptionID$", mVocation.ID.ToString());
                }
                else
                {
                    str = str.Replace("$VocationOptionID$", mVocation.ID.ToString());
                    str = str.Replace(" selected=selected", ""); 
                }
                str = str.Replace("$VocationOptionName$", mVocation.VocName);
                sbVocOption.Append(str);
            }
            return sbVocOption.ToString();
        }
        /// <summary>
        /// 显示婚姻状态
        /// </summary>
        /// <param name="MarriageID">婚姻状态ID</param>
        /// <param name="sbHtml">主StringBuilder</param>
        /// <returns></returns>
        private StringBuilder Marriage(int MarriageID, StringBuilder sbHtml)
        {
            switch (MarriageID)
            {
                case 0:
                    sbHtml = sbHtml.Replace("$selected0$", "selected")
                        .Replace("$selected1$", "")
                        .Replace("$selected2$", "")
                        .Replace("$selected3$", "")
                        .Replace("$selected4$", "")
                        .Replace("$selected5$", "");
                    break;
                case 1:
                    sbHtml = sbHtml.Replace("$selected0$", "")
                        .Replace("$selected1$", "selected")
                        .Replace("$selected2$", "")
                        .Replace("$selected3$", "")
                        .Replace("$selected4$", "")
                        .Replace("$selected5$", "");
                    break;
                case 2:
                    sbHtml = sbHtml.Replace("$selected0$", "")
                        .Replace("$selected1$", "")
                        .Replace("$selected2$", "selected")
                        .Replace("$selected3$", "")
                        .Replace("$selected4$", "")
                        .Replace("$selected5$", "");
                    break;
                case 3:
                    sbHtml = sbHtml.Replace("$selected0$", "")
                        .Replace("$selected1$", "")
                        .Replace("$selected2$", "")
                        .Replace("$selected3$", "selected")
                        .Replace("$selected4$", "")
                        .Replace("$selected5$", "");
                    break;
                case 4:
                    sbHtml = sbHtml.Replace("$selected0$", "")
                        .Replace("$selected1$", "")
                        .Replace("$selected2$", "")
                        .Replace("$selected3$", "")
                        .Replace("$selected4$", "selected")
                        .Replace("$selected5$", "");
                    break;
                case 5:
                    sbHtml = sbHtml.Replace("$selected0$", "")
                        .Replace("$selected1$", "")
                        .Replace("$selected2$", "")
                        .Replace("$selected3$", "")
                        .Replace("$selected4$", "")
                        .Replace("$selected5$", "selected");
                    break;
            }
            return sbHtml;
        }
    }
}
